﻿using adb;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PowerStation.RequestModel
{
    /// <summary>
    /// 分类搜索
    /// </summary>
    public class PageModel
    {
        /// <summary>
        /// 当前分布,默认为1
        /// </summary>
        public int PageIndex { get; set; } = 1;
        /// <summary>
        /// 分页大小,默认为10
        /// </summary>
        public int PageSize { get; set; } = 10;
        /// <summary>
        /// 配置分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        public int setPage<T>(ref IQueryable<T> query)
        {
            int count = query.Count();
            query = query.Skip((PageIndex - 1) * PageSize).Take(PageSize);
            return count;
        }
        /// <summary>
        /// 配置分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<Tuple<int,IQueryable<T>>> setPageAsync<T>(IQueryable<T> query)
        {
            int count = await query.CountAsync();
            query = query.Skip((PageIndex - 1) * PageSize).Take(PageSize);
            return new Tuple<int, IQueryable<T>>(count,query);
        }
        /// <summary>
        /// 配置分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<Tuple<int, IQueryable<T>>> setPageAsync<T>(List<T> query)
        {
            int count = query.Count;
            //query = query.Skip((PageIndex - 1) * PageSize).Take(PageSize);
            var query1 = query.AsQueryable().Skip((PageIndex - 1) * PageSize).Take(PageSize);
            return new Tuple<int, IQueryable<T>>(count, query1);
        }
    }
}
